package com.redsoil.house.ui.matter.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;
import com.redsoil.house.R;
import com.redsoil.house.base.RootActivity;
import com.redsoil.house.ui.matter.contract.MatterPhotoContract;
import com.redsoil.house.ui.matter.presenter.MatterPhotoPresenter;
import com.redsoil.house.widget.actionbar.XBaseTopBar;
import com.redsoil.house.widget.actionbar.XBaseTopBarImp;

import butterknife.BindView;
import me.relex.photodraweeview.PhotoDraweeView;

public class MatterPhotoActivity extends RootActivity<MatterPhotoPresenter> implements MatterPhotoContract.View, XBaseTopBarImp{

    @BindView(R.id.main_topbar)
    XBaseTopBar main_topbar;

    @BindView(R.id.photo_drawee_view)
    PhotoDraweeView photo_drawee_view;

    @Override
    public void left() {
        finish();
    }

    @Override
    public void right() {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_matter_photo;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initEventAndData() {
        main_topbar.setxBaseTopBarImp(this);
//        photo_drawee_view.setPhotoUri(Uri.parse());
//        photo_drawee_view.setImageURI(getIntent().getStringExtra("url"));



        String img_url = getIntent().getStringExtra("url");
        if (!TextUtils.isEmpty(img_url)) {
            PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
            controller.setUri(img_url);//设置图片url
            controller.setOldController(photo_drawee_view.getController());
            controller.setControllerListener(new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                    super.onFinalImageSet(id, imageInfo, animatable);
                    if (imageInfo == null || photo_drawee_view == null) {
                        return;
                    }
                    photo_drawee_view.update(imageInfo.getWidth(), imageInfo.getHeight());
                }
            });
            photo_drawee_view.setController(controller.build());
        } else {
            Toast.makeText(this, "图片获取失败", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 打开新Activity
     *
     * @param context
     * @return
     */
    public static Intent newInstance(Context context, String url) {
        Intent intent = new Intent(context, MatterPhotoActivity.class);
        intent.putExtra("url",url);
        return intent;
    }
}
